import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RenRen {
	private URL url = null;
	private HttpURLConnection httpurlconnection = null;
	private String name;
	private String passwd;
	private String check;
	public static final String loginURL = "http://www.renren.com/PLogin.do";
	public static final String stateURL = "http://status.renren.com/doing/update.do";

	public RenRen() {
		this.name = "囧";
		this.passwd = "123456";
		this.check = "123456";
	}

	public RenRen(String name, String passwd) {
		this.name = name;
		this.passwd = passwd;
		this.check = "123456";
	}

	public String outPutStream(InputStream urlStream) throws Exception {
		BufferedInputStream buff = new BufferedInputStream(urlStream);
		Reader r = new InputStreamReader(buff, "utf-8");
		BufferedReader br = new BufferedReader(r);
		StringBuffer strHtml = new StringBuffer("");
		String strLine = null;
		while ((strLine = br.readLine()) != null) {
			strHtml.append(strLine + "\r\n");
		}
		return strHtml.toString();
	}

	public void setCheck(String toCheck) {
		String regEx = "get_check:'([^']+)'";
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(toCheck);
		boolean rs = m.find();
		for (int i = 1; i <= m.groupCount(); i++) {
			check = m.group(i);
		}
	}

	public void login() throws Exception {
		// Cookie handler, 设置 cookie 代理，否则会提示无法开启 session 错误
		CookieManager manager = new CookieManager();
		manager.setCookiePolicy(java.net.CookiePolicy.ACCEPT_ALL);
		CookieHandler.setDefault(manager);

		url = new URL(loginURL);
		String strPost = "email=" + name + "&password=" + passwd
				+ "&autoLogin=true&origURL=http%3A%2F%2Fwww.renren.com"
				+ "%2FHome.do&domain=renren.com";// 请求的参数

		httpurlconnection = (HttpURLConnection) url.openConnection();
		httpurlconnection.setDoOutput(true); // 需要向服务器写数据
		httpurlconnection.setDoInput(true);
		httpurlconnection.setUseCaches(false); // 获得服务器最新的信息
		httpurlconnection.setAllowUserInteraction(false);
		httpurlconnection.setRequestMethod("POST");
		httpurlconnection.setRequestProperty("Accept-Language", "zh-cn");
		httpurlconnection.setRequestProperty("User-Agent",
				"Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
		httpurlconnection.getOutputStream().write(strPost.getBytes()); // 将参数写入链接中
		httpurlconnection.getOutputStream().flush();
		httpurlconnection.getOutputStream().close();
		httpurlconnection.connect(); // 链接
		InputStream urlStream = httpurlconnection.getInputStream(); // 返回结果输出流

		String toCheck = outPutStream(urlStream);
		setCheck(toCheck);

		httpurlconnection.disconnect();
	}

	public void changeState(String newState) throws IOException {
		URL urlState = new URL(stateURL);
		String state = "c=" + newState + "&raw=" + newState
				+ "&isAtHome=1&publisher_form_ticket=" + check
				+ "&requestToken=" + check;
		httpurlconnection = (HttpURLConnection) urlState.openConnection();
		httpurlconnection.setDoOutput(true); // 需要向服务器写数据
		httpurlconnection.setDoInput(true);
		httpurlconnection.setUseCaches(false); // 获得服务器最新的信息
		httpurlconnection.setAllowUserInteraction(false);
		httpurlconnection.setRequestMethod("POST");
		httpurlconnection.setRequestProperty("Accept-Language", "zh-cn");
		httpurlconnection.setRequestProperty("User-Agent",
				"Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
		httpurlconnection.setRequestProperty("referer",
				"http://status.renren.com/ajaxproxy.htm");
		httpurlconnection.getOutputStream().write(state.getBytes()); // 将参数写入链接中
		httpurlconnection.getOutputStream().flush();
		httpurlconnection.getOutputStream().close();
		httpurlconnection.connect(); // 链接
		System.out.println("code" + httpurlconnection.getResponseCode()); // 请求响应结果
		httpurlconnection.disconnect();
	}

	public String reverse(String str) {
		StringBuffer sb = new StringBuffer(str);
		return sb.reverse().toString();
	}

	public static void main(String[] args) throws Exception {
		RenRen me = new RenRen("email", "passwd");
		me.login();
		me.changeState("RT 团子小(囧)：" + me.reverse(args[0]));
	}
}